<template>
	<view>
		<!-- 天气信息显示区域 -->
		<view v-if="weatherData">
			<!-- 城市信息 -->
			<view>
				<text>城市: {{ weatherData.cityInfo.city }}</text>
			</view>
			
			<!-- 明天天气预报 -->
			<view>
				<text>明天天气预报</text>
				<view>
					<text>日期: {{ tomorrowData.ymd }}</text>
					<text>星期: {{ tomorrowData.week }}</text>
					<text>最高温: {{ tomorrowData.high }}</text>
					<text>最低温: {{ tomorrowData.low }}</text>
				</view>
			</view>
		</view>
		
		<!-- 加载状态 -->
		<view v-if="loading">
			<text>正在获取天气数据...</text>
		</view>
		
		<!-- 错误提示 -->
		<view v-if="error">
			<text>{{ error }}</text>
		</view>
		
		<!-- 获取天气按钮 -->
		<button @click="getWeather">获取天气预报</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				weatherData: null,
				tomorrowData: {},
				loading: false,
				error: ''
			}
		},
		methods: {
			getWeather() {
				this.loading = true;
				this.error = '';
				
				uni.request({
					url: 'http://t.weather.sojson.com/api/weather/city/101230501',
					success: (res) => {
						console.log("success-----" + JSON.stringify(res));
						this.weatherData = res.data;
						
						// 获取明天的天气预报数据（数组第二个元素）
						if (this.weatherData.data.forecast && this.weatherData.data.forecast.length > 1) {
							this.tomorrowData = this.weatherData.data.forecast[1];
						}
					},
					fail: (eMsg) => {
						console.log("request fail-----" + eMsg);
						this.error = "获取天气数据失败";
					},
					complete: () => {
						this.loading = false;
					}
				});
			}
		}
	}
</script>

<style>
</style>